home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / NextAnswers / 1337_ni_connections < prev    next >
Text File  |  1993-09-23  |  5KB  |  216 lines

  1. #! /bin/sh -u
  2. #
  3. # @(#)ni_connections $Revision: 1.2 $ $Date: 93/07/23 19:18:13 $
  4. # $Author: amm $, $Locker:  $: $State: Rel1 $
  5. # $Source: /Users/amm/ni_connections/RCS/ni_connections.sh,v $
  6. #
  7. # Update log at end of file
  8. #
  9. # ni_connections: show extant NetInfo connections
  10. #
  11. # Copyright 1993 by NeXT Computer, Inc.  All rights reserved.
  12.  
  13. PATH=/usr/bin:/bin:/usr/ucb:/usr/etc:/etc
  14. readonly PATH
  15.  
  16. ECHO=echo
  17. ERROR=$ECHO
  18. WARNING=$ECHO
  19. DEBUG=$ECHO
  20. TRUE=true
  21. FALSE=false
  22. BASENAME=basename
  23. RM=rm
  24. CAT=cat
  25. readonly ECHO ERROR WARNING DEBUG TRUE FALSE BASENAME RM CAT
  26.  
  27. AWK=awk
  28. NIDOMAIN=nidomain
  29. BM=bm
  30. NETSTAT=netstat
  31. SNMPNETSTAT=snmpnetstat
  32. SORT=sort
  33. HOSTNAME=hostname
  34. SED=sed
  35. readonly AWK NIDOMAIN BM NETSTAT SNMPNETSTAT SORT HOSTNAME SED
  36.  
  37. LOCALTAG="tag=local"
  38. TEMP=/tmp/Bindings-$$
  39. readonly LOCALTAG TEMP
  40.  
  41. # General return codes
  42. E_OK=0
  43. E_ShellError=1
  44. E_BadSyntax=2
  45. E_Intr=3
  46. E_Quit=4
  47. E_Term=5
  48. readonly E_OK E_ShellError E_BadSyntax E_Intr E_Quit E_Term
  49.  
  50. E_NStat=6
  51. readonly E_NStat
  52.  
  53. D_ArgV=localhost
  54. D_SkipLocal=$TRUE
  55. D_NumericInfo=$FALSE
  56. D_NumericFlag=
  57. readonly D_ArgV D_SkipLocal D_NumericInfo D_NumericFlag
  58.  
  59. cleanup=$TRUE
  60.  
  61. PGMNAME=`$BASENAME ${0} .sh`
  62. USAGE=\
  63. "Usage: ${PGMNAME} [-l] [-n] [host]
  64.     Use ${PGMNAME} -help for details"
  65. EXPLANATION="Arguments:
  66.     -l    Show connections to the local domain
  67.     -n    Use and show numeric addresses, instead of names
  68.     host    Name of host whose connections should be checked
  69.  
  70. Defaults: $D_ArgV"
  71. EXAMPLES="${PGMNAME}
  72. ${PGMNAME} -l rhino"
  73. readonly PGMNAME USAGE EXPLANATION EXAMPLES
  74.  
  75. case ${1-NotHelp} in
  76. -help | -HELP | -Help)    # User wants more info
  77.     $ERROR 1>&2 "${USAGE}"
  78.     # For proper formatting, ${EXPLANATION} *MUST* be double-quoted, below
  79.     # If you remove the double-quotes, add '\n's to the end of each line
  80.     # in the definition of EXPLANATION, above.
  81.     $ERROR 1>&2 "${EXPLANATION}"
  82.     $ERROR 1>&2 "Examples: ${EXAMPLES}"
  83.     exit ${E_OK}
  84.     ;;
  85. esac
  86.  
  87. # Assume all will be well.
  88. exitcode=$E_OK
  89.  
  90. # Signal handlers
  91. # Normal termination handler; helps ensure clean exit
  92. trap \
  93.     "if \$cleanup; then
  94.         $RM -f $TEMP;
  95.      fi
  96.         exit \$exitcode" \
  97.     0
  98. # Handler for SIGHUP and SIGINT.  Print a message and clean up.
  99. trap \
  100.     "$ERROR 1>&2 'Interrupted; cleaning up.';
  101.         exitcode=$E_Intr; exit" \
  102.     1 2
  103. # Handler for SIGQUIT.  Leave the temp files for autopsy.
  104. trap \
  105.     "$ERROR 1>&2 'QUIT received; temp file $TEMP remains.';
  106.         cleanup = $FALSE;
  107.         exitcode=$E_Quit; exit" \
  108.     3
  109. # Handler for SIGTERM.  Clean up.
  110. trap \
  111.     "$ERROR 1>&2 'Terminated; cleaning up.';
  112.         exitcode=$E_Term; exit" \
  113.     15
  114.  
  115. while [ $# -gt 0 ]; do
  116.     case ${1} in
  117.     -l ) # Show the local domain
  118.     SkipLocal=$FALSE
  119.     ;;
  120.     -n ) # Numeric information
  121.     NumericInfo=$TRUE
  122.     NumericFlag=-n
  123.     ;;
  124.     -* ) # Unknown flag
  125.     $ERROR 1>&2 "${PGMNAME}: unknown flag ${1}"
  126.     $ERROR 1>&2 "${USAGE}"
  127.     exitcode=${E_BadSyntax}; exit
  128.     ;;
  129.     *)    # Arguments follow
  130.     break
  131.     ;;
  132.     esac
  133.     shift
  134. done
  135.  
  136. host=${@-$D_ArgV}
  137. SkipLocal=${SkipLocal-$D_SkipLocal}
  138. NumericInfo=${NumericInfo-$D_NumericInfo}
  139. NumericFlag=${NumericFlag-$D_NumericFlag}
  140.  
  141. is_local=$FALSE
  142.  
  143. if [ "$host" != "localhost" ]; then
  144.     for h in `$HOSTNAME`; do
  145.     if [ "$h" = "$host" ]; then
  146.         is_local=$TRUE
  147.         break
  148.     fi
  149.     done
  150. else
  151.     is_local=$TRUE
  152. fi
  153.  
  154. if $is_local; then
  155.     NSTAT="$NETSTAT $NumericFlag"
  156.     args=""
  157. else
  158.     NSTAT="$SNMPNETSTAT $NumericFlag"
  159.     args=$host
  160. fi
  161.  
  162. $NSTAT $args | $SORT +4 > $TEMP
  163. status=$?
  164. if [ 0 -ne $status ]; then
  165.     $ERROR 1>&2 "Cannot get connection information; $NSTAT status $status"
  166.     exitcode=$E_NStat
  167.     exit $exitcode
  168. fi
  169.  
  170. # $TEMP now has netstat sorted by remote port.  There's some garbage in
  171. # there, though; e.g. (note that this has been sorted, and isn't in the
  172. # order netstat prints it):
  173. #
  174. #Active Internet connections
  175. #udp        0      0  localhost.ntp          *.*                   
  176. #udp        0      0  nescorna.ntp           *.*                   
  177. #Proto Recv-Q Send-Q  Local Address          Foreign Address        (state)
  178. #
  179. # We'll get rid of it by filtering in awk for tcp or udp and not *.*
  180.  
  181. for port in `$AWK \
  182.     '/^[tu][cd]p/ && $5 !~ /\*\.\*/    {printf "%s.%s\n", $5, $1}' $TEMP`; do
  183.     if $NumericInfo; then
  184.         # port has REMOTEIP.PORTNUM.TYPE, e.g., 192.42.172.5.666.tcp
  185.     # We want the IP Address in $1, the port in $2, and the type in $3
  186.     set `$ECHO "$port" | $SED 's/^\(..*\)\.\(..*\)\.\(..*\)$/\1 \2 \3/'`
  187.     else
  188.     # port has REMOTEHOST.PORTNUM.TYPE, e.g., rhino.666.tcp
  189.     # We want the hostname in $1, port in $2, and type in $3
  190.         set `$ECHO "$port" | $AWK -f. '{printf "%s %s %s", $1, $2, $3}'`
  191.     fi
  192.     line=`$NIDOMAIN -l $1 | $BM "$3=$2"`
  193.     if [ -n "$line" ]; then
  194.     case "$line" in
  195.     ${LOCALTAG}* ) # This line refers to the local domain
  196.         if $SkipLocal; then
  197.         continue
  198.         fi
  199.         ;;
  200.     esac
  201.     $ECHO "$line" | $AWK '{print $1}' | \
  202.         $AWK -F= '{printf "connected to %s on ", $2}'
  203.     $ECHO "$1 via $3 port $2"
  204.     fi
  205. done
  206.  
  207. exit $exitcode
  208.  
  209. # $Log:    ni_connections.sh,v $
  210. # Revision 1.2  93/07/23  19:18:13  amm
  211. # Modifications for numeric option, and for release with NSiF.
  212. # Revision 1.1  93/03/30  17:49:36  amm
  213. # Initial revision
  214.